package com.yeqifu.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeqifu.bus.entity.Goods;
import com.yeqifu.bus.entity.GoodsDate;
import com.yeqifu.bus.entity.Inport;
import com.yeqifu.bus.entity.Outport;
import com.yeqifu.bus.mapper.GoodsDateMapper;
import com.yeqifu.bus.mapper.GoodsMapper;
import com.yeqifu.bus.mapper.InportMapper;
import com.yeqifu.bus.mapper.OutportMapper;
import com.yeqifu.bus.service.IGoodsDateService;
import com.yeqifu.bus.service.IGoodsService;
import com.yeqifu.bus.service.IOutportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeqifu.bus.vo.InportVo;
import com.yeqifu.bus.vo.OutportVo;
import com.yeqifu.sys.common.WebUtils;
import com.yeqifu.sys.entity.Shelf;
import com.yeqifu.sys.entity.User;
import com.yeqifu.sys.mapper.ShelfMapper;
import com.yeqifu.sys.service.IShelfService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * InnoDB free: 9216 kB 服务实现类
 * </p>
 *
 * @author 艾琦
 * @since 2020-12-19
 */
@Service
@AllArgsConstructor
public class OutportServiceImpl extends ServiceImpl<OutportMapper, Outport> implements IOutportService {

    OutportMapper outportMapper;
    GoodsMapper goodsMapper;
    GoodsDateMapper goodsDateMapper;
    ShelfMapper shelfMapper;

    IGoodsService goodsService;
    IGoodsDateService goodsDateService;
    IShelfService shelfService;



    @Override
    public boolean recallById(Outport entity,int type) {
        //根据进货ID查询进货信息
        Outport outport = baseMapper.selectById(entity.getId());
        int status = outport.getStatus();
        // 判断是否是业务管理员 且 未处理 || 是否是仓库管理员 且 未处理/已处理
        if ((type == 0 && status == 0) || (type == 1 && (status == 1 || status == 0)))
            return super.updateById(entity);
        // 判断
        else
            return false;
    }

    @Override
    public boolean realSave(OutportVo outportVo) {
        System.out.println("我被执行了1");
        // 找到此条出库信息
        Outport outport = outportMapper.selectById(outportVo.getId());
        System.out.println(outport+"+++++++++");
        // 判断状态是否是未处理
        if (outport.getStatus() != 0)
            return false;
        // 1、查询商品表，将此货架、此商品名称的数量--
        Goods goods = goodsMapper.selectById(outport.getGoodsid());
        goods.setNumber(goods.getNumber()-outport.getNumber());
        goodsService.updateById(goods);
        // 2、查询货架表，使用算法将此类型货架的数量--
        Shelf shelf = shelfMapper.selectById(goods.getLocationid());
        shelf.setCapacity(shelf.getCapacity()-outport.getNumber());
        shelfService.updateById(shelf);
        // 3、查询商品详细表，查询出所有此货架、此商品的信息，并根据保质期排序，存入到 Map<id，num> 中，将此Map中数量--
        QueryWrapper<GoodsDate> goodsDateQueryWrapper = new QueryWrapper<>();
        goodsDateQueryWrapper.eq("name",goods.getGoodsname()).eq("locationid",goods.getLocationid()).orderByAsc("qualdate");
        List<GoodsDate> goodsDates = goodsDateService.list(goodsDateQueryWrapper);
        // 出库数
        int outPortNumber = outport.getNumber();
        for (GoodsDate goodsDate : goodsDates) {
            System.out.println("出库数"+outport.getNumber() + "入库数" + goodsDate.getNum());
            // 从头开始将数量--
            int num = goodsDate.getNum();
            // 此条进货信息数量足够的情况下
            if ((num - outPortNumber) >= 0) {
                goodsDate.setNum(goodsDate.getNum() - outPortNumber);
                goodsDateService.updateById(goodsDate);
                // 退出循环
                break;
            } else { // 此条进货信息数量不足的情况下
                outPortNumber = outPortNumber - num;
                goodsDate.setNum(0);
                goodsDateService.updateById(goodsDate);
            }
        }
        // 4、如果出库成功修改订单状态为已处理
        outport.setStatus(2);
        return super.updateById(outport);
    }


    /**
     * 业务管理员更新商品出库
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(Outport entity) {
        //根据进货ID查询进货信息
        Outport outport = baseMapper.selectById(entity.getId());
        // 判断状态，是否是“未处理的状态”
        if (outport.getStatus() != 0)
            return false;
        //更新进货单
        return super.updateById(entity);
    }

    /**
     * 查询商品月出库情况(近似于销售情况)
     * @return
     */
    @Override
    public List<Outport> loadMonthSales(Integer id) {
        List<Outport> outport = baseMapper.loadMonthSales(id);
        return outport;
    }
    /**
     * 查询商品年出库情况(近似于销售情况)
     * @return
     */
    @Override
    public List<Outport> loadYearSales() {
        List<Outport> outport = baseMapper.loadYearSales();
        return outport;
    }

    /**
     * 业务管理员修改订单信息，只能修改状态为 0 的订单
     * @param entity
     * @return
     */

}
